using System.Collections.Generic;
using System.Data.SqlClient;
using DatabaseUnitTesting.Utilities.Tests;
using NUnit.Framework;

namespace DatabaseUnitTesting.Utilities.DataCompare.Tests
{
    [TestFixture]
    public class DiffReaderTests
    {
        private DataComparer _fastDataComparer;
        private string _databaseOne;
        private string _databaseTwo;
        private SqlConnection _connection;

        [TestFixtureSetUp]
        public void TestFixtureSetUp()
        {
            string server = "(local)";
            string database = "astyler";
            string connectionString = "Application Name=UnitTests;Trusted_Connection=true;Server=" + server +
                                      ";Database=" + database;
            _connection = new SqlConnection(connectionString);
            _connection.Open();
            _databaseOne = "comparer1";
            _databaseTwo = "comparer2";
        }

        [Test]
        public void GenerateNoDifferencesTest()
        {
            _fastDataComparer = new DataComparer(_connection, _databaseOne, _databaseTwo);
            Assert.AreEqual(0, _fastDataComparer.GenerateDifferences(null).TableCount);
           
        }

        [Test]
        public void GenerateTwoDifferencesTest()
        {
            _fastDataComparer = new DataComparer(_connection, _databaseOne, _databaseTwo);
            _fastDataComparer.AddObjectComparison("dbo", "unitTestShortView", "dbo", "unitTestTable");

            Database diffs = _fastDataComparer.GenerateDifferences(null);
            Assert.AreEqual(1, diffs.TableCount);
            foreach(KeyValuePair<Table, int> pair in diffs.Tables)
            {
                Assert.AreEqual(2, pair.Key.RowCount);
            }
        }

        [Test]
        public void CompareDatabasesTest()
        {
            _fastDataComparer = new DataComparer(_connection, "astyler", "astyler1");
           
                _fastDataComparer.AddObjectComparison("dbo", "t100k", "dbo", "t100k");

                XmlFileAdapter.Write("C:/Temp/CompareDatabasesTestFast.xml", _fastDataComparer.GenerateDifferences(null));

                Assert.IsTrue(
                    _fastDataComparer.GenerateDifferences(null).Equals(XmlFileAdapter.Read("C:/Temp/CompareDatabasesTestFast.xml")));
            
        }

        [Test]
        public void TableViewVerificationTests()
        {
            _fastDataComparer = new DataComparer(_connection, "comparer1", "comparer2");

            _fastDataComparer.AddObjectComparison("dbo", "unitTestTable", "dbo", "unitTestTable");

            _fastDataComparer.AddObjectComparison("dbo", "unitTestTable", "dbo", "fakeTable");

            CustomAssert.AssertException(delegate { _fastDataComparer.GenerateDifferences(null); },
                                         typeof(SqlException));
        }

        [TestFixtureTearDown]
        public void tearDown()
        {
            _connection.Close();
        }
    }
}
